package com.bci.pwtz.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bci.pwtz.common.PwtzConstants;
import com.bci.pwtz.common.util.PaginationList;
import com.bci.pwtz.common.util.PwtzUtils;
import com.bci.pwtz.exception.PwtzDatabaseException;
import com.bci.pwtz.exception.PwtzException;
import com.bci.pwtz.mysql.model.Company;
import com.bci.pwtz.mysql.model.CompanyMessage;
import com.bci.pwtz.mysql.model.CompanyUser;
import com.bci.pwtz.mysql.model.CompanyUserAudit;
import com.bci.pwtz.mysql.model.CompanyUserRole;
import com.bci.pwtz.mysql.model.UserCompanyRef;
import com.bci.pwtz.service.CompanyMessageService;
import com.bci.pwtz.service.CompanyRoleService;
import com.bci.pwtz.service.CompanyService;
import com.bci.pwtz.service.CompanyUserAuditService;
import com.bci.pwtz.service.CompanyUserService;
import com.bci.pwtz.service.MessageService;
import com.bci.pwtz.service.UserCompanyRefService;

@Controller
@RequestMapping("/user/companymessage")
public class CompanyMessageController extends AbstractController{
	@Autowired
    private CompanyMessageService     companyMessageService;
	@Autowired
	private CompanyUserAuditService  companyUserAuditService;
	@Autowired
	private CompanyUserService  companyUserService;
	@Autowired
	private MessageService messageService;
	@Autowired
	private CompanyRoleService  companyRoleService;
	@Autowired
	private CompanyService  companyService;
	@Autowired
    private UserCompanyRefService  userCompanyRefService;
	
	@RequestMapping(value="/list",method=RequestMethod.POST)
    @ResponseBody
    public PaginationList<CompanyMessage> list(int pageszie,int pagenum,long companyId,long userId) throws PwtzException{
        
        PaginationList<CompanyMessage> list = new PaginationList<CompanyMessage>();
        try
        {
            String companyName=companyService.load(companyId).getCompanyName();
            list = companyMessageService.selectCompanyMessages(pageszie, pagenum,companyName,userId);
        }
        catch (PwtzDatabaseException e)
        {
            e.printStackTrace();
        }
        
        return list;
    }
	
	@RequestMapping(value="/useraudit",method=RequestMethod.POST)
    @ResponseBody
    public CompanyUserAudit useraudit(long auditId) throws PwtzException{
        
		CompanyUserAudit companyUserAudit = new CompanyUserAudit();
        try
        {
        	companyUserAudit = companyUserAuditService.selectCompanyUserAuditByAuditId(auditId);
        }
        catch (PwtzDatabaseException e)
        {
            e.printStackTrace();
        }
        
        return companyUserAudit;
    }
	
	@RequestMapping(value="/refuse",method=RequestMethod.POST)
    @ResponseBody
    public Map<String, String> refuse(long auditId,long messageId) throws PwtzException{
        
		Map<String, String> respMap = new HashMap<String, String>();
        try
        {
        	CompanyUserAudit companyUserAudit = new CompanyUserAudit();
        	companyUserAudit.setAuditId(auditId);
        	companyUserAudit.setResult(3);
        	int res = companyUserAuditService.update(companyUserAudit);
            //System.out.println("res="+res);
            if (res == 1){
            	respMap = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR,null, "操作成功");
            }else{
            	respMap = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR,null, "操作失败");
            }
            CompanyMessage message=new CompanyMessage();
            message.setMessageId(messageId);
            message.setReadStatus(2);
            companyMessageService.update(message);
        }
        catch (PwtzDatabaseException e)
        {
        	respMap = packageException(e);
        }
        return respMap;
    }
	
	@RequestMapping(value="/del",method=RequestMethod.POST)
    @ResponseBody
    public Map<String, String> del(String messageId) throws PwtzException{
        
		Map<String, String> respMap = new HashMap<String, String>();
        try
        {
        	String[] message = messageId.split(",");
        	for(int i=0;i<message.length;i++){
        		int res = companyMessageService.delete(PwtzUtils.passLong(message[i]));
        		if (res == 1){
                	respMap = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR,null, "操作成功");
                }else{
                	respMap = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR,null, "操作失败");
                }
        	}
        }
        catch (PwtzDatabaseException e)
        {
        	respMap = packageException(e);
        }
        return respMap;
    }
	
	@RequestMapping(value="/examine",method=RequestMethod.POST)
    @ResponseBody
    public Map<String, String> examine(long auditId,long messageId) throws PwtzException{
        
		Map<String, String> respMap = new HashMap<String, String>();
        try
        {
        	CompanyUserAudit companyUserAudit = companyUserAuditService.selectCompanyUserAuditByAuditId(auditId);
        	boolean isVip =  companyService.isVip(companyUserAudit.getCompanyId());
        	if(!isVip){
        	    int companyUsers = userCompanyRefService.selectCompanyUsers(companyUserAudit.getCompanyId());
        	    if(companyUsers>=PwtzConstants.THE_TRIAL_USERS_COUNT){
        	        respMap = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR,null, "审核失败，单位员工数已满");
        	        return respMap;
        	    }
        	}
        	long companyRoleId = companyRoleService.selectCompanyRoleId(companyUserAudit.getCompanyId());
        	//System.out.println("getCompanyUserId="+companyUserAudit.getCompanyUserId());
        	//System.out.println("companyRoleId="+companyRoleId);
        	//System.out.println("getCompanyId="+companyUserAudit.getCompanyId());
        	if(companyRoleService.checkExistsCompanyUserRole(companyUserAudit.getCompanyUserId(), companyRoleId)){
        	    companyUserAudit.setAuditId(auditId);
                companyUserAudit.setResult(3);
                int ret = companyUserAuditService.update(companyUserAudit);
                if(ret==1)
                    respMap = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR,null, "已经加入过该公司");
                else
                    respMap = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR,null, "审核失败"); 
        	}else{
        		if(companyRoleService.checkExistsUserCompanyRef(companyUserAudit.getCompanyUserId(), companyUserAudit.getCompanyId())){
        		    companyUserAudit.setAuditId(auditId);
                    companyUserAudit.setResult(3);
                    int ret = companyUserAuditService.update(companyUserAudit); 
                    if(ret==1)
                        respMap = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR,null, "已经加入过该公司");
                    else
                        respMap = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR,null, "审核失败");
        		}else{
        			UserCompanyRef userCompanyRef = new UserCompanyRef();
        			userCompanyRef.setCompanyId(companyUserAudit.getCompanyId());
        			userCompanyRef.setCompanyUserId(companyUserAudit.getCompanyUserId());
        			int ret = companyUserService.addUserCompanyRef(userCompanyRef);
        			if(ret == 1){
	        			CompanyUserRole companyUserRole = new CompanyUserRole();
	        			companyUserRole.setCompanyUserId(companyUserAudit.getCompanyUserId());
	        			companyUserRole.setCompanyRoleId(companyRoleId);
	        			ret = companyUserService.addCompanyUserRole(companyUserRole);
	        			if(ret == 1){
	        				companyUserAudit.setAuditId(auditId);
	        	        	companyUserAudit.setResult(2);
	        	        	ret = companyUserAuditService.update(companyUserAudit);
	        	        	CompanyUser user=companyUserService.load(companyUserAudit.getCompanyUserId());
                            if(user.getLastCompanyId()==null){
                                user.setLastCompanyId(companyUserAudit.getCompanyId());
                                companyUserService.update(user);
                            }
	        	        	if(ret == 1){
	        	        		Company company = companyService.load(companyUserAudit.getCompanyId());
	        	        		messageService.sendAuditSmsAndMsg(companyUserAudit.getCompanyUserId(), auditId, PwtzConstants.AUDIT_SUCCESS_MSG, company.getCompanyName());
	        	        		respMap = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR,null, "审核成功");
	        	        	}else{
	        	        		respMap = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR,null, "审核失败");
	        	        	}
	        			}else{
	        			    respMap = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR,null, "审核失败");
	        			}
        			}else{
        				respMap = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR,null, "审核失败");
        			}
        		}
        	}
        	CompanyMessage message=new CompanyMessage();
            message.setMessageId(messageId);
            message.setReadStatus(2);
            companyMessageService.update(message);
        }
        catch (PwtzDatabaseException e)
        {
        	respMap = packageException(e);
        }
        return respMap;
    }
}
